package akash.java.code;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Intershop
{
    public static void main(String[] args)
    {
        try
        {
            Intershop intershop = new Intershop();
            InputStream is = new FileInputStream("D:/intershop.txt");
            StringBuffer text = new StringBuffer();
            InputStreamReader isr = new InputStreamReader(is);

            int ch;
            while((ch = isr.read()) != -1)
            {
                char letter = (char)ch;
                text.append(letter);
            }
            Map<String, Integer> textMap = new HashMap<String, Integer>();
            textMap = intershop.createMap(text);

            intershop.printMap(textMap);
            OutputStream os = new FileOutputStream("D:/intershop_stats.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("stats");
            XSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("Word");
            row.createCell(1).setCellValue("Frequency");
            intershop.writeMapToExcel(textMap, sheet);
            workbook.write(os);
            os.close();

        }
        catch(IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void printMap(Map<String, Integer> textMap)
    {
        Set<Entry<String, Integer>> textSet = textMap.entrySet();
        for(Iterator<Entry<String, Integer>> iterator = textSet.iterator(); iterator.hasNext();)
        {
            String word = (String)iterator.next().toString();
            System.out.println(word);
        }

    }

    private Map<String, Integer> createMap(StringBuffer text)
    {
        Map<String, Integer> textMap = new TreeMap<String, Integer>();
        StringBuffer word = new StringBuffer();
        List<String> wordList = new ArrayList<String>();

        for(int i = 0; i < text.length(); i++)
        {
            char letter = text.charAt(i);
            // int listSize = (int)((wordList.size()>0)? wordList.size():1);
            if (letter == 32)
            {
                if (wordList.size() == 0)
                {
                    continue;
                }
                else if (wordList.get(wordList.size() - 1) != " ")
                {  
                    wordList.add(word.toString());
                    word = new StringBuffer();
                    continue;
                }

              
                continue;
            }
            else if (i == text.length() - 1)
            {
                if (letter != 32)
                {
                    word.append(letter);
                }
                wordList.add(word.toString());
                continue;
            }
            else
            {
                word.append(letter);
            }
        }
        Collections.sort(wordList);
        String string = "";

        int count = 1;

        for(int i = 0; i < wordList.size(); i++)
        {
            string = wordList.get(i);
            if (textMap.containsKey(string))
            {
                int existingCount = textMap.get(string);
                textMap.put(string, count + existingCount);
            }
            else
            {
                textMap.put(string, count);
            }
        }

        return textMap;
    }

    private void writeMapToExcel(Map textMap, XSSFSheet sheet)

    {
        String word = "";
        Set keySet = textMap.keySet();
        Iterator wordIterator = keySet.iterator();
        for(int i = 0; i < keySet.size(); i++)
        {
            XSSFRow row = sheet.createRow(i + 1);
            word = (String)wordIterator.next();
            row.createCell(0).setCellValue(word);
            row.createCell(1).setCellValue((int)textMap.get(word));
        }
    }

}
